package com.example.bittraffic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ActivityGoogleMap03 extends MapActivity {
	ProgressDialog loagindDialog;
	MapView mapView;
	double nowLat;
	double nowLong;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_activity_google_map03);

		mapView = (MapView) findViewById(R.id.mapview3);
		mapView.setVisibility(View.GONE);

		getLocat();

//		Button mapShowbtn = (Button) findViewById(R.id.mapshow3);
//		mapShowbtn.setOnClickListener(clickListener);

	}

	Button.OnClickListener clickListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.mapshow3:
				showMap();
			}
		}
	};

	private void showMap() {
		mapView.setVisibility(View.VISIBLE);
		Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		InterestringLocations funPlaces = new InterestringLocations(marker);
		mapView.getOverlays().add(funPlaces);

		ClickReceiver clickRecvt = new ClickReceiver(this, funPlaces);
		mapView.getOverlays().add(clickRecvt);
	}

	public class ClickReceiver extends Overlay {
		private Context context;
		InterestringLocations temp;

		public ClickReceiver(Context _context, InterestringLocations temp_) {
			context = _context;
			temp = temp_;
		}

		@Override
		public boolean onTap(GeoPoint p, MapView mapView) {
			// TODO Auto-generated method stub
			GeoPoint b = new GeoPoint((int) p.getLongitudeE6(),	(int) p.getLatitudeE6());
			mapView.getController().animateTo(b);
			temp.clear();
			temp.addOverlay(new OverlayItem(b, "test", "test"));

			myAddress.setText("위치 :" + (double) p.getLatitudeE6() / 1000000
					+ "," + (double) p.getLongitudeE6() / 1000000);

			return true;
		}
	}

	class InterestringLocations extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> locations = new ArrayList<OverlayItem>();
		private Drawable marker;

		public InterestringLocations(Drawable marker) {
			super(marker);
			this.marker = marker;

			int latitude = (int) (nowLat * 1E6);
			int longitude = (int) (nowLong * 1E6);

			GeoPoint gp = new GeoPoint(latitude, longitude);
			mapView.getController().setCenter(gp);
			mapView.getController().setZoom(19);
			locations.add(new OverlayItem(gp, "test", "test"));

			populate();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			// TODO Auto-generated method stub
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			// TODO Auto-generated method stub
			return locations.get(i);
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return locations.size();
		}

		public void addOverlay(OverlayItem overlay) {
			locations.add(overlay);
			setLastFocusedIndex(-1);
			populate();
		}

		public void clear() {
			locations.clear();
			mapView.removeAllViews();
			setLastFocusedIndex(-1);
			populate();
		}
	}

	public void getLocat() {
		// TODO Auto-generated method stub
//		loagindDialog = ProgressDialog.show(ActivityGoogleMap03.this,
//				"현재위치 설정중 ...", "Please wait...", true, false);
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						MapView mapView = (MapView) findViewById(R.id.mapview3);
						final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(
								ActivityGoogleMap03.this, mapView);
						myLocationOverlay.enableMyLocation();
						
						if (myLocationOverlay.isMyLocationEnabled()) {
							myLocationOverlay.runOnFirstFix(new Runnable() {
								@Override
								public void run() {
									// TODO Auto-generated method stub
									double nowLat = 0;
									double nowLong = 0;
									
									nowLat = (double) myLocationOverlay
											.getMyLocation().getLatitudeE6() / 1000000f;
									nowLong = (double) myLocationOverlay
											.getMyLocation().getLongitudeE6() / 100000f;
//									mHandler.sendEmptyMessageDelayed(0, 10);
									Log.e("Message", nowLat +","+ nowLong);
								}
							});
						}
					}
				});
			}
		}).start();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
//			loagindDialog.dismiss();
//			String address = getAddress(nowLat, nowLong);
//			myAddress = (TextView) findViewById(R.id.mylocation3);
//			myAddress.setText(address);
		}
	};

	TextView myAddress;

	public String getAddress(double lat, double longi) {
		String result = null;
		Geocoder geoCoder = new Geocoder(ActivityGoogleMap03.this);

		List<Address> addresses = null;
		try {
			addresses = geoCoder.getFromLocation(lat, longi, 5);
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (addresses != null && addresses.size() > 0) {
			Address mAddress = addresses.get(0);
			result = mAddress.getAddressLine(0);
		} else {
			result = "현재 위치를 표시 할 수 없습니다";
		}
		return result;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
		System.exit(0);
		super.onBackPressed();
	}
}